package com.cn.customer.housekeeping.housekeepingb.ui.base;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.customer.housekeeping.housekeepingb.EBApplication;
import com.cn.customer.housekeeping.housekeepingb.R;
import com.cn.customer.housekeeping.housekeepingb.biz.BasePresenter;
import com.cn.customer.housekeeping.housekeepingb.biz.IMvpView;
import com.cn.customer.housekeeping.housekeepingb.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingb.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingb.bridge.http.OkHttpManager;
import com.cn.customer.housekeeping.housekeepingb.util.DialogUtil;
import com.cn.customer.housekeeping.housekeepingb.view.LoadingDialog;
import com.umeng.analytics.MobclickAgent;


/**
 * <基础activity>
 */
public abstract class BaseActivity extends Activity implements CreateInit, PublishActivityCallBack, PresentationLayerFunc, IMvpView, OnClickListener {
    public TextView tvTopTitle, tvTopRighttitle;
    public ImageView ivTopBack,ivTopAdd;
    public RelativeLayout rlTitle;

    protected String[] needPermissions = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,

    };
    private PresentationLayerFuncHelper presentationLayerFuncHelper;


    public BasePresenter presenter;

    public final String TAG = this.getClass().getSimpleName();
    public RelativeLayout mRlTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        presentationLayerFuncHelper = new PresentationLayerFuncHelper(this);
        initViews();
        initListeners();
        setHeader();
        initData();
        EBApplication.getInstance().addActivity(this);

    }


    @Override
    public void setHeader() {
        rlTitle = (RelativeLayout) findViewById(R.id.rl_title);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        tvTopRighttitle = (TextView) findViewById(R.id.tv_top_righttitle);
        ivTopAdd= (ImageView) findViewById(R.id.iv_top_add);
        ivTopBack.setOnClickListener(this);
        tvTopRighttitle.setOnClickListener(this);
        ivTopAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_top_back:
                finish();
//                overridePendingTransition(R.anim.slide_left_in,
//                        R.anim.slide_right_out);
                break;
//            case R.id.iv_top_personal_center:
//                NavUtils.gotoConversationActivity(this);
//                break;

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        EBApplication.getInstance().currentActivityName = this.getClass().getName();
        MobclickAgent.onPageStart(EBApplication.getInstance().currentActivityName);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(EBApplication.getInstance().currentActivityName);
        MobclickAgent.onPause(this);
    }

    @Override
    public void startActivity(Class<?> openClass, Bundle bundle) {
        Intent intent = new Intent(this, openClass);
        if (null != bundle)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void openActivityForResult(Class<?> openClass, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, openClass);
        if (null != bundle)
            intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void setResultOk(Bundle bundle) {
        Intent intent = new Intent();
        if (bundle != null) ;
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void showToast(String msg) {
        presentationLayerFuncHelper.showToast(msg);
    }

    @Override
    public void showProgressDialog() {
        presentationLayerFuncHelper.showProgressDialog();
    }

    @Override
    public void hideProgressDialog() {
        presentationLayerFuncHelper.hideProgressDialog();
    }

    @Override
    public void showSoftKeyboard(View focusView) {
        presentationLayerFuncHelper.showSoftKeyboard(focusView);
    }

    @Override
    public void hideSoftKeyboard() {
        presentationLayerFuncHelper.hideSoftKeyboard();
    }

    @Override
    protected void onDestroy() {
        EBApplication.getInstance().deleteActivity(this);
        if (presenter != null) {
            presenter.detachView(this);
        }
        OkHttpManager httpManager = BridgeFactory.getBridge(Bridges.HTTP);
//        httpManager.cancelActivityRequest(TAG);
        super.onDestroy();
    }

    /**
     * 加载状态条
     */
    protected LoadingDialog mLoadingDialog;

    /**
     * 显示状态加载条
     *
     * @param msg 提示信息
     */
    public void showLoadingView(String msg) {
        if (mLoadingDialog == null) {
            if (BaseActivity.this instanceof Activity) {
                mLoadingDialog = DialogUtil.showLoadingDialog(BaseActivity.this, msg, true);
            } else {
                return;
            }
        }

        mLoadingDialog.setDlgMessage(msg);

        if (BaseActivity.this instanceof Activity) {
            Activity ac = BaseActivity.this;
            if (ac == null || ac.isFinishing()) {
                return;
            }
        }
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    /**
     * 隐藏状态加载提示
     */
    public void hideLoadingView() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void showLoading() {
        showLoadingView("正在加载");
    }

    @Override
    public void hideLoading() {
        hideLoadingView();
    }


//    @Override
//    public void onRequestPermissionsResult(int requestCode,
//                                           String[] permissions, int[] paramArrayOfInt) {
//        System.out.println("wjm===onRequestPermissionsResult22222");
//        if (requestCode == Utils.PERMISSON_REQUESTCODE) {
//            if (!Utils.verifyPermissions(paramArrayOfInt)) {
//                DialogUtil.showMissingPermissionDialog(getApplicationContext());
//                isNeedCheck = false;
//            }
//        }
//    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
//            overridePendingTransition(R.anim.slide_left_in,
//                    R.anim.slide_right_out);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
